#pragma semicolon 1
#include <sourcemod>
#include <sdktools>

#include ps_natives

#define PLUGIN_VERSION "1.0"
#define PS_ModuleName "\nBuy Extended Support Structure(BESS Module)"

new Handle:h_Enable = INVALID_HANDLE;
new Handle:h_Trie = INVALID_HANDLE;
new bool:loaded = false;

public Plugin:myinfo = 
{
	name = "[PS] Buy Extended Support Structure(BESS Module)",
	author = "McFlurry",
	description = "Module to extend buy support, example: !buy pills // this would buy you pills",
	version = PLUGIN_VERSION,
	url = "N/A"
}

public OnPluginStart()
{
	h_Trie = CreateTrie();
	decl String:game_name[64];
	GetGameFolderName(game_name, sizeof(game_name));
	if (!StrEqual(game_name, "left4dead2", false))
	{
		SetFailState("Plugin supports Left 4 Dead 2 only.");
	}
	CreateConVar("ps_bess_version", PLUGIN_VERSION, "Version of bess module", FCVAR_PLUGIN|FCVAR_DONTRECORD|FCVAR_NOTIFY|FCVAR_REPLICATED);
	h_Enable = CreateConVar("ps_bess_enable", "1", "Enable BESS Module", FCVAR_PLUGIN, true, 0.0, true, 1.0);
	RegConsoleCmd("sm_buy", Cmd_Buy);
	/* RegConsoleCmd("sm_buyhelp", Cmd_Help); */
	AutoExecConfig(true, "ps_bess");
}

public OnAllPluginsLoaded()
{
	new Float:min = 1.59;
	if(LibraryExists("ps_natives"))
	{
		if(PS_GetVersion() >= min)
		{
			PS_RegisterModule(PS_ModuleName);
			SetUpBuyTrie();
			loaded = true;
		}	
		else
		{
			SetFailState("[PS] Incompatible version of points system is loaded! Please update!");
		}	
	}
	else
	{
		SetFailState("[PS] PS Natives aren't loaded!");
	}	
}

public OnPluginEnd()
{
	PS_UnregisterModule(PS_ModuleName);
	CloseHandle(h_Trie);
}	

public OnConfigsExecuted()
{
	UpdateBuyTrie();
}	

public SetUpBuyTrie()
{
		//health
	SetTrieString(h_Trie, "zty", "give pain_pills");
	SetTrieValue(h_Trie, "ztycost", GetConVarInt(FindConVar("l4d2_points_pills")));
	SetTrieString(h_Trie, "jjb", "give first_aid_kit");
	SetTrieValue(h_Trie, "jjbcost", GetConVarInt(FindConVar("l4d2_points_kit")));
	SetTrieString(h_Trie, "djq", "give defibrillator");
	SetTrieValue(h_Trie, "djqcost", GetConVarInt(FindConVar("l4d2_points_defib")));
	SetTrieString(h_Trie, "ssxs", "give adrenaline");
	SetTrieValue(h_Trie, "ssxscost", GetConVarInt(FindConVar("l4d2_points_adrenaline")));
	SetTrieString(h_Trie, "fheal", "give health");
	//SetTrieValue(h_Trie, "fhealcost", GetConVarInt(FindConVar(""))) inapplicable for fheal since cost is dependant on 2 different cvars for different teams
	//secondaries
	SetTrieString(h_Trie, "sq", "give pistol");
	SetTrieValue(h_Trie, "sqcost", GetConVarInt(FindConVar("l4d2_points_pistol")));
	SetTrieString(h_Trie, "smzy", "give pistol_magnum");
	SetTrieValue(h_Trie, "smzycost", GetConVarInt(FindConVar("l4d2_points_magnum")));
	//smgs
	SetTrieString(h_Trie, "qxcfq", "give smg");
	SetTrieValue(h_Trie, "qxcfqcost", GetConVarInt(FindConVar("l4d2_points_smg")));
	SetTrieString(h_Trie, "xscfq", "give smg_silenced");
	SetTrieValue(h_Trie, "xscfqcost", GetConVarInt(FindConVar("l4d2_points_ssmg")));
	SetTrieString(h_Trie, "mp5cfq", "give smg_mp5");
	SetTrieValue(h_Trie, "mp5cfqcost", GetConVarInt(FindConVar("l4d2_points_mp5")));
	//rifles
	SetTrieString(h_Trie, "m16bq", "give rifle");
	SetTrieValue(h_Trie, "m16bqcost", GetConVarInt(FindConVar("l4d2_points_m16")));
	SetTrieString(h_Trie, "scbq", "give rifle_desert");
	SetTrieValue(h_Trie, "scbqcost", GetConVarInt(FindConVar("l4d2_points_scar")));
	SetTrieString(h_Trie, "ak47bq", "give rifle_ak47");
	SetTrieValue(h_Trie, "ak47bqcost", GetConVarInt(FindConVar("l4d2_points_ak")));
	SetTrieString(h_Trie, "sg552bq", "give rifle_sg552");
	SetTrieValue(h_Trie, "sg552bqcost", GetConVarInt(FindConVar("l4d2_points_sg")));
	SetTrieString(h_Trie, "m60bq", "give rifle_m60");
	SetTrieValue(h_Trie, "m60bqcost", GetConVarInt(FindConVar("l4d2_points_m60")));
	//snipers
	SetTrieString(h_Trie, "sljjq", "give hunting_rifle");
	SetTrieValue(h_Trie, "sljjqcost", GetConVarInt(FindConVar("l4d2_points_hunting_rifle")));
	SetTrieString(h_Trie, "bjjq", "give sniper_scout");
	SetTrieValue(h_Trie, "bjjqcost", GetConVarInt(FindConVar("l4d2_points_scout")));
	SetTrieString(h_Trie, "jsjjq", "give sniper_military");
	SetTrieValue(h_Trie, "jsjjqcost", GetConVarInt(FindConVar("l4d2_points_military_sniper")));
	SetTrieString(h_Trie, "awpjjq", "give sniper_awp");
	SetTrieValue(h_Trie, "awpjjqcost", GetConVarInt(FindConVar("l4d2_points_awp")));
	//shotguns
	SetTrieString(h_Trie, "ljsdq", "give shotgun_chrome");
	SetTrieValue(h_Trie, "ljsdqcost", GetConVarInt(FindConVar("l4d2_points_chrome")));
	SetTrieString(h_Trie, "tgsdq", "give pumpshotgun");
	SetTrieValue(h_Trie, "tgsdqcost", GetConVarInt(FindConVar("l4d2_points_pump")));
	SetTrieString(h_Trie, "jdsdq", "give shotgun_spas");
	SetTrieValue(h_Trie, "jdsdqcost", GetConVarInt(FindConVar("l4d2_points_spas")));
	SetTrieString(h_Trie, "zdsdq", "give autoshotgun");
	SetTrieValue(h_Trie, "zdsdqcost", GetConVarInt(FindConVar("l4d2_points_autoshotgun")));
	//throwables
	SetTrieString(h_Trie, "rsp", "give molotov");
	SetTrieValue(h_Trie, "rspcost", GetConVarInt(FindConVar("l4d2_points_molotov")));
	SetTrieString(h_Trie, "tzd", "give pipe_bomb");
	SetTrieValue(h_Trie, "tzdcost", GetConVarInt(FindConVar("l4d2_points_pipe")));
	SetTrieString(h_Trie, "dzp", "give vomitjar");
	SetTrieValue(h_Trie, "dzpcost", GetConVarInt(FindConVar("l4d2_points_bile")));
	//misc
	SetTrieString(h_Trie, "dj", "give chainsaw");
	SetTrieValue(h_Trie, "djcost", GetConVarInt(FindConVar("l4d2_points_chainsaw")));
	SetTrieString(h_Trie, "ldq", "give grenade_launcher");
	SetTrieValue(h_Trie, "ldqcost", GetConVarInt(FindConVar("l4d2_points_grenade")));
	SetTrieString(h_Trie, "xar", "give gnome");
	SetTrieValue(h_Trie, "xarcost", GetConVarInt(FindConVar("l4d2_points_gnome")));
	SetTrieString(h_Trie, "klp", "give cola_bottles");
	SetTrieValue(h_Trie, "klpcost", GetConVarInt(FindConVar("l4d2_points_cola")));
	SetTrieString(h_Trie, "qyt", "give gascan");
	SetTrieValue(h_Trie, "qytcost", GetConVarInt(FindConVar("l4d2_points_gascan")));
	SetTrieString(h_Trie, "bwqg", "give propanetank");
	SetTrieValue(h_Trie, "bwqgcost", GetConVarInt(FindConVar("l4d2_points_propane")));
	SetTrieString(h_Trie, "yhh", "give fireworkcrate");
	SetTrieValue(h_Trie, "yhhcost", GetConVarInt(FindConVar("l4d2_points_fireworks")));
	SetTrieString(h_Trie, "yqg", "give oxygentank");
	SetTrieValue(h_Trie, "yqgcost", GetConVarInt(FindConVar("l4d2_points_oxygen")));
	//upgrades
	SetTrieString(h_Trie, "gbdb", "give upgradepack_explosive");
	SetTrieValue(h_Trie, "gbdbcost", GetConVarInt(FindConVar("l4d2_points_explosive_ammo_pack")));
	SetTrieString(h_Trie, "rsdb", "give upgradepack_incendiary");
	SetTrieValue(h_Trie, "rsdbcost", GetConVarInt(FindConVar("l4d2_points_incendiary_ammo_pack")));
	SetTrieString(h_Trie, "zd", "give ammo");
	SetTrieValue(h_Trie, "zdcost", GetConVarInt(FindConVar("l4d2_points_refill")));
	SetTrieString(h_Trie, "gbd", "upgrade_add EXPLOSIVE_AMMO");
	SetTrieValue(h_Trie, "gbdcost", GetConVarInt(FindConVar("l4d2_points_explosive_ammo")));
	SetTrieString(h_Trie, "rsd", "upgrade_add INCENDIARY_AMMO");
	SetTrieValue(h_Trie, "rsdcost", GetConVarInt(FindConVar("l4d2_points_incendiary_ammo")));
	SetTrieString(h_Trie, "jgmz", "upgrade_add LASER_SIGHT");
	SetTrieValue(h_Trie, "jgmzcost", GetConVarInt(FindConVar("l4d2_points_laser")));
	//melee
	SetTrieString(h_Trie, "tq", "give crowbar");
	SetTrieValue(h_Trie, "tqcost", GetConVarInt(FindConVar("l4d2_points_crowbar")));
	SetTrieString(h_Trie, "bqp", "give cricket_bat");
	SetTrieValue(h_Trie, "bqpcost", GetConVarInt(FindConVar("l4d2_points_cricketbat")));
	SetTrieString(h_Trie, "bqg", "give baseball_bat");
	SetTrieValue(h_Trie, "bqgcost", GetConVarInt(FindConVar("l4d2_points_bat")));
	SetTrieString(h_Trie, "xgd", "give machete");
	SetTrieValue(h_Trie, "xgdcost", GetConVarInt(FindConVar("l4d2_points_machete")));
	SetTrieString(h_Trie, "jg", "give tonfa");
	SetTrieValue(h_Trie, "jgcost", GetConVarInt(FindConVar("l4d2_points_tonfa")));
	SetTrieString(h_Trie, "wsd", "give katana");
	SetTrieValue(h_Trie, "wsdcost", GetConVarInt(FindConVar("l4d2_points_katana")));
	SetTrieString(h_Trie, "xff", "give fireaxe");
	SetTrieValue(h_Trie, "xffcost", GetConVarInt(FindConVar("l4d2_points_fireaxe")));
	SetTrieString(h_Trie, "djt", "give electric_guitar");
	SetTrieValue(h_Trie, "djtcost", GetConVarInt(FindConVar("l4d2_points_guitar")));
	SetTrieString(h_Trie, "pdg", "give frying_pan");
	SetTrieValue(h_Trie, "pdgcost", GetConVarInt(FindConVar("l4d2_points_pan")));
	SetTrieString(h_Trie, "gefqg", "give golfclub");
	SetTrieValue(h_Trie, "gefqgcost", GetConVarInt(FindConVar("l4d2_points_golfclub")));
	//infected
	SetTrieString(h_Trie, "zs", "kill");
	SetTrieValue(h_Trie, "zscost", GetConVarInt(FindConVar("l4d2_points_suicide")));
	SetTrieString(h_Trie, "pz", "z_spawn boomer auto");
	SetTrieValue(h_Trie, "pzcost", GetConVarInt(FindConVar("l4d2_points_boomer")));
	SetTrieString(h_Trie, "yg", "z_spawn smoker auto");
	SetTrieValue(h_Trie, "ygcost", GetConVarInt(FindConVar("l4d2_points_smoker")));
	SetTrieString(h_Trie, "ls", "z_spawn hunter auto");
	SetTrieValue(h_Trie, "lscost", GetConVarInt(FindConVar("l4d2_points_hunter")));
	SetTrieString(h_Trie, "ks", "z_spawn spitter auto");
	SetTrieValue(h_Trie, "kscost", GetConVarInt(FindConVar("l4d2_points_spitter")));
	SetTrieString(h_Trie, "hz", "z_spawn jockey auto");
	SetTrieValue(h_Trie, "hzcost", GetConVarInt(FindConVar("l4d2_points_jockey")));
	SetTrieString(h_Trie, "zn", "z_spawn charger auto");
	SetTrieValue(h_Trie, "zncost", GetConVarInt(FindConVar("l4d2_points_charger")));
	SetTrieString(h_Trie, "nw", "z_spawn witch auto");
	SetTrieValue(h_Trie, "nwcost", GetConVarInt(FindConVar("l4d2_points_witch")));
	SetTrieString(h_Trie, "xn", "z_spawn witch_bride auto");
	SetTrieValue(h_Trie, "xncost", GetConVarInt(FindConVar("l4d2_points_witch")));
	SetTrieString(h_Trie, "tkk", "z_spawn tank auto");
	SetTrieValue(h_Trie, "tkkcost", GetConVarInt(FindConVar("l4d2_points_tank")));
	SetTrieString(h_Trie, "sc", "director_force_panic_event");
	SetTrieValue(h_Trie, "sccost", GetConVarInt(FindConVar("l4d2_points_horde")));
	SetTrieString(h_Trie, "sq", "z_spawn mob auto");
	SetTrieValue(h_Trie, "sqcost", GetConVarInt(FindConVar("l4d2_points_mob")));
	SetTrieString(h_Trie, "qhsq", "z_spawn mob");
	SetTrieValue(h_Trie, "qhsqcost", GetConVarInt(FindConVar("l4d2_points_umob")));
}	

public UpdateBuyTrie()
{
	//health
	SetTrieValue(h_Trie, "ztycost", GetConVarInt(FindConVar("l4d2_points_pills")), true);
	SetTrieValue(h_Trie, "jjbcost", GetConVarInt(FindConVar("l4d2_points_kit")), true);
	SetTrieValue(h_Trie, "djqcost", GetConVarInt(FindConVar("l4d2_points_defib")), true);
	SetTrieValue(h_Trie, "ssxscost", GetConVarInt(FindConVar("l4d2_points_adrenaline")), true);
	//SetTrieValue(h_Trie, "fhealcost", GetConVarInt(FindConVar(""))) inapplicable for fheal since cost is dependant on 2 different cvars for different teams
	//secondaries
	SetTrieValue(h_Trie, "sqcost", GetConVarInt(FindConVar("l4d2_points_pistol")), true);
	SetTrieValue(h_Trie, "smzycost", GetConVarInt(FindConVar("l4d2_points_magnum")), true);
	//smgs
	SetTrieValue(h_Trie, "qxcfqcost", GetConVarInt(FindConVar("l4d2_points_smg")), true);
	SetTrieValue(h_Trie, "xscfqcost", GetConVarInt(FindConVar("l4d2_points_ssmg")), true);
	SetTrieValue(h_Trie, "mp5cfqcost", GetConVarInt(FindConVar("l4d2_points_mp5")), true);
	//rifles
	SetTrieValue(h_Trie, "m16bqcost", GetConVarInt(FindConVar("l4d2_points_m16")), true);
	SetTrieValue(h_Trie, "scbqcost", GetConVarInt(FindConVar("l4d2_points_scar")), true);
	SetTrieValue(h_Trie, "ak47bqcost", GetConVarInt(FindConVar("l4d2_points_ak")), true);
	SetTrieValue(h_Trie, "sg552bqcost", GetConVarInt(FindConVar("l4d2_points_sg")), true);
	SetTrieValue(h_Trie, "m60bqcost", GetConVarInt(FindConVar("l4d2_points_m60")), true);
	//snipers
	SetTrieValue(h_Trie, "sljjqcost", GetConVarInt(FindConVar("l4d2_points_hunting_rifle")), true);
	SetTrieValue(h_Trie, "bjjqcost", GetConVarInt(FindConVar("l4d2_points_scout")), true);
	SetTrieValue(h_Trie, "jsjjqcost", GetConVarInt(FindConVar("l4d2_points_military_sniper")), true);
	SetTrieValue(h_Trie, "awpjjqcost", GetConVarInt(FindConVar("l4d2_points_awp")), true);
	//shotguns
	SetTrieValue(h_Trie, "ljsdqcost", GetConVarInt(FindConVar("l4d2_points_chrome")), true);
	SetTrieValue(h_Trie, "tgsdqcost", GetConVarInt(FindConVar("l4d2_points_pump")), true);
	SetTrieValue(h_Trie, "jdsdqcost", GetConVarInt(FindConVar("l4d2_points_spas")), true);
	SetTrieValue(h_Trie, "zdsdqcost", GetConVarInt(FindConVar("l4d2_points_autoshotgun")), true);
	//throwables
	SetTrieValue(h_Trie, "rspcost", GetConVarInt(FindConVar("l4d2_points_molotov")), true);
	SetTrieValue(h_Trie, "tzdcost", GetConVarInt(FindConVar("l4d2_points_pipe")), true);
	SetTrieValue(h_Trie, "dzpcost", GetConVarInt(FindConVar("l4d2_points_bile")), true);
	//misc
	SetTrieValue(h_Trie, "djcost", GetConVarInt(FindConVar("l4d2_points_chainsaw")), true);
	SetTrieValue(h_Trie, "ldqcost", GetConVarInt(FindConVar("l4d2_points_grenade")), true);
	SetTrieValue(h_Trie, "xarcost", GetConVarInt(FindConVar("l4d2_points_gnome")), true);
	SetTrieValue(h_Trie, "klpcost", GetConVarInt(FindConVar("l4d2_points_cola")), true);
	SetTrieValue(h_Trie, "qytcost", GetConVarInt(FindConVar("l4d2_points_gascan")), true);
	SetTrieValue(h_Trie, "bwqgcost", GetConVarInt(FindConVar("l4d2_points_propane")), true);
	SetTrieValue(h_Trie, "yhhcost", GetConVarInt(FindConVar("l4d2_points_fireworks")), true);
	SetTrieValue(h_Trie, "yqgcost", GetConVarInt(FindConVar("l4d2_points_oxygen")), true);
	//upgrades
	SetTrieValue(h_Trie, "gbdbcost", GetConVarInt(FindConVar("l4d2_points_explosive_ammo_pack")), true);
	SetTrieValue(h_Trie, "rsdbcost", GetConVarInt(FindConVar("l4d2_points_incendiary_ammo_pack")), true);
	SetTrieValue(h_Trie, "zdcost", GetConVarInt(FindConVar("l4d2_points_refill")), true);
	SetTrieValue(h_Trie, "gbdcost", GetConVarInt(FindConVar("l4d2_points_explosive_ammo")), true);
	SetTrieValue(h_Trie, "rsdcost", GetConVarInt(FindConVar("l4d2_points_incendiary_ammo")), true);
	SetTrieValue(h_Trie, "jgmzcost", GetConVarInt(FindConVar("l4d2_points_laser")), true);
	//melee
	SetTrieValue(h_Trie, "tqcost", GetConVarInt(FindConVar("l4d2_points_crowbar")), true);
	SetTrieValue(h_Trie, "bqpcost", GetConVarInt(FindConVar("l4d2_points_cricketbat")), true);
	SetTrieValue(h_Trie, "bqgcost", GetConVarInt(FindConVar("l4d2_points_bat")), true);
	SetTrieValue(h_Trie, "xgdcost", GetConVarInt(FindConVar("l4d2_points_machete")), true);
	SetTrieValue(h_Trie, "jgcost", GetConVarInt(FindConVar("l4d2_points_tonfa")), true);
	SetTrieValue(h_Trie, "wsdcost", GetConVarInt(FindConVar("l4d2_points_katana")), true);
	SetTrieValue(h_Trie, "xffcost", GetConVarInt(FindConVar("l4d2_points_fireaxe")), true);
	SetTrieValue(h_Trie, "djtcost", GetConVarInt(FindConVar("l4d2_points_guitar")), true);
	SetTrieValue(h_Trie, "pdgcost", GetConVarInt(FindConVar("l4d2_points_pan")), true);
	SetTrieValue(h_Trie, "gefqgcost", GetConVarInt(FindConVar("l4d2_points_golfclub")), true);
	//infected
	SetTrieValue(h_Trie, "zscost", GetConVarInt(FindConVar("l4d2_points_suicide")), true);
	SetTrieValue(h_Trie, "pzcost", GetConVarInt(FindConVar("l4d2_points_boomer")), true);
	SetTrieValue(h_Trie, "ygcost", GetConVarInt(FindConVar("l4d2_points_smoker")), true);
	SetTrieValue(h_Trie, "lscost", GetConVarInt(FindConVar("l4d2_points_hunter")), true);
	SetTrieValue(h_Trie, "kscost", GetConVarInt(FindConVar("l4d2_points_spitter")), true);
	SetTrieValue(h_Trie, "hzcost", GetConVarInt(FindConVar("l4d2_points_jockey")), true);
	SetTrieValue(h_Trie, "zncost", GetConVarInt(FindConVar("l4d2_points_charger")), true);
	SetTrieValue(h_Trie, "nwcost", GetConVarInt(FindConVar("l4d2_points_witch")), true);
	SetTrieValue(h_Trie, "xncost", GetConVarInt(FindConVar("l4d2_points_witch")), true);
	SetTrieValue(h_Trie, "tkkcost", GetConVarInt(FindConVar("l4d2_points_tank")), true);
	SetTrieValue(h_Trie, "sccost", GetConVarInt(FindConVar("l4d2_points_horde")), true);
	SetTrieValue(h_Trie, "sqcost", GetConVarInt(FindConVar("l4d2_points_mob")), true);
	SetTrieValue(h_Trie, "qhsqcost", GetConVarInt(FindConVar("l4d2_points_umob")), true);
}

public Action:Cmd_Buy(client, args)
{
	if(!GetConVarBool(h_Enable) || !loaded || !IsClientInGame(client) || client > MaxClients) return Plugin_Continue;
	if(!IsPlayerAlive(client) && args > 0)
	{
		ReplyToCommand(client, "[鸭蛋]请在灵魂状态或复活后使用积分系统!");
		return Plugin_Continue;
	}	
	if(args > 1 || args == 0) return Plugin_Continue;
	new String:arg[50];
	GetCmdArg(1, arg, sizeof(arg));
	new String:argval[100];
	if(!GetTrieString(h_Trie, arg, argval, sizeof(argval)))
	{
		return Plugin_Continue;
	}
	else
	{
		new icost = -2; //-2 = invalid
		if(StrEqual(arg, "cola", false))
		{
			new String:map[100];
			GetCurrentMap(map, 100);
			if(StrEqual(map, "c1m2_streets", false))
			{
				PrintToChat(client, "[PS] This item is unavailable during this map");
				return Plugin_Continue;
			}
		}	
		if(StrEqual(arg, "fheal", false) && GetClientTeam(client) == 3)
		{
			if(GetEntProp(client, Prop_Send, "m_zombieClass") == 8) PS_SetCost(client, GetConVarInt(FindConVar("l4d2_points_infected_heal"))*GetConVarInt(FindConVar("l4d2_points_tank_heal_mult")));
			else PS_SetBoughtCost(client, GetConVarInt(FindConVar("l4d2_points_infected_heal")));
			PS_SetBought(client, argval);
			HandleHeal(client);
		}	
		else if(StrEqual(arg, "fheal", false) && GetClientTeam(client) == 2)
		{
			PS_SetBoughtCost(client, GetConVarInt(FindConVar("l4d2_points_survivor_heal")));
			PS_SetBought(client, argval);
			HandleHeal(client);
		}	
		if(StrEqual(arg, "kill", false))
		{
			Format(arg, sizeof(arg), "%scost", arg);
			GetTrieValue(h_Trie, arg, icost);
			PS_SetBoughtCost(client, icost);
			PS_SetBought(client, "suicide");
			HandleSuicide(client);
		}
		else if(StrEqual(arg, "umob", false))
		{
			Format(arg, sizeof(arg), "%scost", arg);
			GetTrieValue(h_Trie, arg, icost);
			PS_SetBoughtCost(client, icost);
			PS_SetBought(client, argval);
			HandleUMob(client);
		}
		else
		{
			PS_SetBought(client, argval);
			Format(arg, sizeof(arg), "%scost", arg);
			GetTrieValue(h_Trie, arg, icost);
			PS_SetBoughtCost(client, icost);
			if(PS_GetBoughtCost(client) == -2) return Plugin_Continue;
			else HandlePurchase(client);
		}	
	}
	return Plugin_Continue;
}	

/* public Action:Cmd_Help(client, args)
{
	if(args > 0 || !loaded || !GetConVarBool(h_Enable) || client == 0 || !IsClientInGame(client)) return Plugin_Handled;
	BuildHelpMenu(client);
	return Plugin_Handled;
}

BuildHelpMenu(client)
{
	if(GetClientTeam(client) == 2)
	{
		decl String:title[40], String:a[40], String:b[40], String:c[40], String:d[40], String:e[40], String:f[40], String:g[40], String:h[40],
		String:i[40], String:j[40], String:k[40], String:l[40], String:m[40], String:n[40], String:o[40], String:p[40], String:q[40], String:r[40],
		String:s[40], String:t[40], String:u[40], String:v[40], String:w[40], String:x[40], String:y[40], String:z[40], String:aa[40], String:ab[40],
		String:ac[40], String:ad[40], String:ae[40], String:af[40], String:ag[40], String:ah[40], String:ai[40], String:aj[40], String:ak[40], String:al[40],
		String:am[40], String:an[40], String:ao[40], String:ap[40], String:aq[40], String:ar[40], String:as[40], String:at[40], String:au[40], String:av[40],
		String:aw[40];
		new Handle:menu = CreateMenu(TopMenu);
		if(GetConVarInt(FindConVar("l4d2_points_pills")) > -1)
		{
			Format(a, sizeof(a),"");
			AddMenuItem(menu, "", a);
		}	
		if(GetConVarInt(FindConVar("l4d2_points_kit")) > -1)
		{
			Format(b, sizeof(b),"");
			AddMenuItem(menu, "", b);
		}	
		if(GetConVarInt(FindConVar("l4d2_points_defib")) > -1)
		{
			Format(a, sizeof(a),"");
			AddMenuItem(menu, "", a);
		}	
		if(GetConVarInt(FindConVar("l4d2_points_adrenaline")) > -1)
		{
			Format(a, sizeof(a),"");
			AddMenuItem(menu, "", a);
		}	
		if(GetConVarInt(FindConVar("l4d2_points_pistol")) > -1)
		{
			Format(a, sizeof(a),"");
			AddMenuItem(menu, "", a);
		}	
		if(GetConVarInt(FindConVar("l4d2_points_magnum")) > -1)
		{
			Format(a, sizeof(a),"");
			AddMenuItem(menu, "", a);
		}	
		if(GetConVarInt(FindConVar("l4d2_points_smg")) > -1)
		{
			Format(a, sizeof(a),"");
			AddMenuItem(menu, "", a);
		}	
		if(GetConVarInt(FindConVar("l4d2_points_ssmg")) > -1)
		{
			Format(a, sizeof(a),"");
			AddMenuItem(menu, "", a);
		}	
		if(GetConVarInt(FindConVar("l4d2_points_mp5")) > -1)
		{
			Format(a, sizeof(a),"");
			AddMenuItem(menu, "", a);
		}	
		if(GetConVarInt(FindConVar("l4d2_points_m16")) > -1)
		{
			Format(a, sizeof(a),"");
			AddMenuItem(menu, "", a);
		}	
		if(GetConVarInt(FindConVar("l4d2_points_scar")) > -1)
		{
			Format(a, sizeof(a),"");
			AddMenuItem(menu, "", a);
		}	
		if(GetConVarInt(FindConVar("l4d2_points_ak")) > -1)
		{
			Format(a, sizeof(a),"");
			AddMenuItem(menu, "", a);
		}	
		if(GetConVarInt(FindConVar("l4d2_points_sg")) > -1)
		{
			Format(a, sizeof(a),"");
			AddMenuItem(menu, "", a);
		}	
		if(GetConVarInt(FindConVar("l4d2_points_m60")) > -1)
		{
			Format(a, sizeof(a),"");
			AddMenuItem(menu, "", a);
		}	
		if(GetConVarInt(FindConVar("l4d2_points_hunting_rifle")) > -1)
		{
			Format(a, sizeof(a),"");
			AddMenuItem(menu, "", a);
		}	
		if(GetConVarInt(FindConVar("l4d2_points_scout")) > -1)
		{
			Format(a, sizeof(a),"");
			AddMenuItem(menu, "", a);
		}	
		if(GetConVarInt(FindConVar("l4d2_points_military_sniper")) > -1)
		{
			Format(a, sizeof(a),"");
			AddMenuItem(menu, "", a);
		}	
		if(GetConVarInt(FindConVar("l4d2_points_awp")) > -1)
		{
			Format(a, sizeof(a),"");
			AddMenuItem(menu, "", a);
		}	
		if(GetConVarInt(FindConVar("l4d2_points_chrome")) > -1)
		{
			Format(a, sizeof(a),"");
			AddMenuItem(menu, "", a);
		}	
		if(GetConVarInt(FindConVar("l4d2_points_pump")) > -1)
		{
			Format(a, sizeof(a),"");
			AddMenuItem(menu, "", a);
		}	
		if(GetConVarInt(FindConVar("l4d2_points_spas")) > -1)
		{
			Format(a, sizeof(a),"");
			AddMenuItem(menu, "", a);
		}	
		if(GetConVarInt(FindConVar("l4d2_points_autoshotgun")) > -1)
		{
			Format(a, sizeof(a),"");
			AddMenuItem(menu, "", a);
		}	
		if(GetConVarInt(FindConVar("l4d2_points_molotov")) > -1)
		{
			Format(a, sizeof(a),"");
			AddMenuItem(menu, "", a);
		}	
		if(GetConVarInt(FindConVar("l4d2_points_pipe")) > -1)
		{
			Format(a, sizeof(a),"");
			AddMenuItem(menu, "", a);
		}	
		if(GetConVarInt(FindConVar("l4d2_points_bile")) > -1)
		{
			Format(a, sizeof(a),"");
			AddMenuItem(menu, "", a);
		}	
		if(GetConVarInt(FindConVar("l4d2_points_chainsaw")) > -1)
		{
			Format(a, sizeof(a),"");
			AddMenuItem(menu, "", a);
		}	
		if(GetConVarInt(FindConVar("l4d2_points_grenade")) > -1)
		{
			Format(a, sizeof(a),"");
			AddMenuItem(menu, "", a);
		}	
		if(GetConVarInt(FindConVar("l4d2_points_gnome")) > -1)
		{
			Format(a, sizeof(a),"");
			AddMenuItem(menu, "", a);
		}	
		if(GetConVarInt(FindConVar("l4d2_points_cola")) > -1)
		{
			Format(a, sizeof(a),"");
			AddMenuItem(menu, "", a);
		}	
		if(GetConVarInt(FindConVar("l4d2_points_gascan")) > -1)
		{
			Format(a, sizeof(a),"");
			AddMenuItem(menu, "", a);
		}	
		if(GetConVarInt(FindConVar("l4d2_points_propane")) > -1)
		{
			Format(a, sizeof(a),"");
			AddMenuItem(menu, "", a);
		}	
		if(GetConVarInt(FindConVar("l4d2_points_fireworks")) > -1)
		{
			Format(a, sizeof(a),"");
			AddMenuItem(menu, "", a);
		}	
		if(GetConVarInt(FindConVar("l4d2_points_oxygen")) > -1)
		{
			Format(a, sizeof(a),"");
			AddMenuItem(menu, "", a);
		}	
		if(GetConVarInt(FindConVar("l4d2_points_explosive_ammo_pack")) > -1)
		{
			Format(a, sizeof(a),"");
			AddMenuItem(menu, "", a);
		}	
		if(GetConVarInt(FindConVar("l4d2_points_incendiary_ammo_pack")) > -1)
		{
			Format(a, sizeof(a),"");
			AddMenuItem(menu, "", a);
		}	
		if(GetConVarInt(FindConVar("l4d2_points_refill")) > -1)
		{
			Format(a, sizeof(a),"");
			AddMenuItem(menu, "", a);
		}	
		if(GetConVarInt(FindConVar("l4d2_points_explosive_ammo")) > -1)
		{
			Format(a, sizeof(a),"");
			AddMenuItem(menu, "", a);
		}	
		if(GetConVarInt(FindConVar("l4d2_points_incendiary_ammo")) > -1)
		{
			Format(a, sizeof(a),"");
			AddMenuItem(menu, "", a);
		}	
		if(GetConVarInt(FindConVar("l4d2_points_laser")) > -1)
		{
			Format(a, sizeof(a),"");
			AddMenuItem(menu, "", a);
		}	
		if(GetConVarInt(FindConVar("l4d2_points_crowbar")) > -1)
		{
			Format(a, sizeof(a),"");
			AddMenuItem(menu, "", a);
		}	
		if(GetConVarInt(FindConVar("l4d2_points_cricketbat")) > -1)
		{
			Format(a, sizeof(a),"");
			AddMenuItem(menu, "", a);
		}	
		if(GetConVarInt(FindConVar("l4d2_points_bat")) > -1)
		{
			Format(a, sizeof(a),"");
			AddMenuItem(menu, "", a);
		}	
		if(GetConVarInt(FindConVar("l4d2_points_machete")) > -1)
		{
			Format(a, sizeof(a),"");
			AddMenuItem(menu, "", a);
		}	
		if(GetConVarInt(FindConVar("l4d2_points_tonfa")) > -1)
		{
			Format(a, sizeof(a),"");
			AddMenuItem(menu, "", a);
		}	
		if(GetConVarInt(FindConVar("l4d2_points_katana")) > -1)
		{
			Format(a, sizeof(a),"");
			AddMenuItem(menu, "", a);
		}	
		if(GetConVarInt(FindConVar("l4d2_points_fireaxe")) > -1)
		{
			Format(a, sizeof(a),"");
			AddMenuItem(menu, "", a);
		}	
		if(GetConVarInt(FindConVar("l4d2_points_guitar")) > -1)
		{
			Format(a, sizeof(a),"");
			AddMenuItem(menu, "", a);
		}	
		if(GetConVarInt(FindConVar("l4d2_points_pan")) > -1)
		{
			Format(a, sizeof(a),"");
			AddMenuItem(menu, "", a);
		}	
		if(GetConVarInt(FindConVar("l4d2_points_golfclub")) > -1)
		{
			Format(a, sizeof(a),"");
			AddMenuItem(menu, "", a);
		}	
		Format(title, sizeof(title),"%d Points Left", points[client]);
		SetMenuTitle(menu, title);
		DisplayMenu(menu, client, MENU_TIME_FOREVER);
	}
	if(GetClientTeam(client) == 3)
	{
		decl String:title[40], String:boomer[40], String:spitter[40], String:smoker[40], String:hunter[40], String:charger[40], String:jockey[40];
		decl String:tank[40], String:witch[40], String:witch_bride[40], String:heal[40], String:suicide[40], String:horde[40], String:mob[40], String:umob[40];
		new Handle:menu = CreateMenu(InfectedMenu);
		SetMenuExitBackButton(menu, true);
		Format(heal, sizeof(heal),"Heal");
		AddMenuItem(menu, "heal", heal);
		Format(suicide, sizeof(suicide),"Suicide");
		AddMenuItem(menu, "suicide", suicide);
		Format(boomer, sizeof(boomer),"Boomer");
		AddMenuItem(menu, "boomer", boomer);
		Format(spitter, sizeof(spitter),"Spitter");
		AddMenuItem(menu, "spitter", spitter);
		Format(smoker, sizeof(smoker),"Smoker");
		AddMenuItem(menu, "smoker", smoker);
		Format(hunter, sizeof(hunter),"Hunter");
		AddMenuItem(menu, "hunter", hunter);
		Format(charger, sizeof(charger),"Charger");
		AddMenuItem(menu, "charger", charger);
		Format(jockey, sizeof(jockey),"jockey");
		AddMenuItem(menu, "jockey", jockey);
		Format(tank, sizeof(tank),"Tank");
		AddMenuItem(menu, "tank", tank);
		if(StrEqual(MapName, "c6m1_riverbank", false))))
		{
			Format(witch_bride, sizeof(witch_bride),"Witch Bride");
			AddMenuItem(menu, "witch_bride", witch_bride);
		}
		else
		{
			Format(witch, sizeof(witch),"Witch");
			AddMenuItem(menu, "witch", witch);
		}	
		Format(horde, sizeof(horde),"Horde");
		AddMenuItem(menu, "horde", horde);
		Format(mob, sizeof(mob),"Mob");
		AddMenuItem(menu, "mob", mob);
		Format(umob, sizeof(umob),"Uncommon Mob");
		AddMenuItem(menu, "uncommon_mob", umob);
		Format(title, sizeof(title),"%d Points Left", points[client]);
		SetMenuTitle(menu, title);
		DisplayMenu(menu, client, MENU_TIME_FOREVER);
	}
}		
 */
stock HandlePurchase(client)
{
	PS_SetCost(client, PS_GetBoughtCost(client));
	if(PS_GetCost(client) > -1 && PS_GetPoints(client) >= PS_GetCost(client))
	{
		RemoveFlags();
		new String:item1[100];
		PS_GetBought(client, item1);
		if(StrEqual(item1, "give ammo", false))
		{
			new wep = GetPlayerWeaponSlot(client, 0);
			if(wep == -1)
			{
				if(IsClientInGame(client)) PrintToChat(client, "[鸭蛋]您必须拥有主武器才能完成填弹!");
				AddFlags();
				return;
			}
			new m60ammo = 150;
			new nadeammo = 70;
			new Handle:cvar = FindConVar("l4d2_guncontrol_m60ammo");
			new Handle:cvar2 = FindConVar("l4d2_guncontrol_grenadelauncherammo");
			if(cvar != INVALID_HANDLE)
			{
				m60ammo = GetConVarInt(cvar);
				CloseHandle(cvar);
			}	
			if(cvar2 != INVALID_HANDLE)
			{
				nadeammo = GetConVarInt(cvar2);
				CloseHandle(cvar2);
			}	
			new String:class[40];
			GetEdictClassname(wep, class, sizeof(class));
			if(StrEqual(class, "weapon_rifle_m60", false)) SetEntProp(wep, Prop_Data, "m_iClip1", m60ammo, 1);
			else if(StrEqual(class, "weapon_grenade_launcher", false))
			{
				new offset = FindDataMapOffs(client, "m_iAmmo");
				SetEntData(client, offset + 68, nadeammo);
			}
		}	
		PS_SetItem(client, item1);
		FakeClientCommand(client, item1);
		AddFlags();
		PS_SetPoints(client, PS_GetPoints(client) - PS_GetCost(client));
	}
	else if(PS_GetCost(client) == -1)
	{
		PrintToChat(client, "[鸭蛋]积分系统失效");
		PS_SetBoughtCost(client, PS_GetBoughtCost(client));
	}
	else
	{
		PS_SetBoughtCost(client, PS_GetBoughtCost(client));
		PrintToChat(client, "[鸭蛋]点数不足");
	}	
}	

stock HandleHeal(client)
{
	PS_SetCost(client, PS_GetBoughtCost(client));
	if(PS_GetCost(client) > -1 && PS_GetPoints(client) >= PS_GetCost(client))
	{
		RemoveFlags();
		new String:item1[100];
		PS_GetBought(client, item1);
		PS_SetItem(client, item1);
		FakeClientCommand(client, item1);
		SetEntPropFloat(client, Prop_Send, "m_healthBuffer", 0.0);
		AddFlags();
		PS_SetPoints(client, PS_GetPoints(client) - PS_GetCost(client));
	}
	else if(PS_GetCost(client) == -1)
	{
		PrintToChat(client, "[鸭蛋]积分系统失效");
		PS_SetBoughtCost(client, PS_GetBoughtCost(client));
	}
	else
	{
		PS_SetBoughtCost(client, PS_GetBoughtCost(client));
		PrintToChat(client, "[鸭蛋]点数不足");
	}	
}	

stock HandleSuicide(client)
{
	PS_SetCost(client, PS_GetBoughtCost(client));
	if(PS_GetCost(client) > -1 && PS_GetPoints(client) >= PS_GetCost(client) && IsClientInGame(client) && IsPlayerAlive(client))
	{
		ForcePlayerSuicide(client);
		PS_SetBought(client, "suicide");
		PS_SetPoints(client, PS_GetPoints(client) - PS_GetCost(client));
	}
	else if(PS_GetCost(client) == -1)
	{
		PrintToChat(client, "[鸭蛋]积分系统失效");
		PS_SetBoughtCost(client, PS_GetBoughtCost(client));
	}
	else
	{
		PS_SetBoughtCost(client, PS_GetBoughtCost(client));
		PrintToChat(client, "[鸭蛋]点数不足");
	}	
}	

stock HandleUMob(client)
{
	PS_SetCost(client, PS_GetBoughtCost(client));
	if(PS_GetCost(client) > -1 && PS_GetPoints(client) >= PS_GetCost(client))
	{
		PS_SetupUMob(GetConVarInt(FindConVar("z_common_limit")));
		PS_SetItem(client, "z_spawn mob");
		PS_SetPoints(client, PS_GetPoints(client) - PS_GetCost(client));
	}
	else if(PS_GetCost(client) == -1)
	{
		PrintToChat(client, "[鸭蛋]积分系统失效");
		PS_SetBoughtCost(client, PS_GetBoughtCost(client));
	}
	else
	{
		PS_SetBoughtCost(client, PS_GetBoughtCost(client));
		PrintToChat(client, "[鸭蛋]点数不足");
	}	
}	

RemoveFlags()
{
	new flagsgive = GetCommandFlags("give");
	new flagszspawn = GetCommandFlags("z_spawn");
	new flagsupgradeadd = GetCommandFlags("upgrade_add");
	new flagspanic = GetCommandFlags("director_force_panic_event");
	SetCommandFlags("give", flagsgive & ~FCVAR_CHEAT);
	SetCommandFlags("z_spawn", flagszspawn & ~FCVAR_CHEAT);
	SetCommandFlags("upgrade_add", flagsupgradeadd & ~FCVAR_CHEAT);
	SetCommandFlags("director_force_panic_event", flagspanic & ~FCVAR_CHEAT);
}	

AddFlags()
{
	new flagsgive = GetCommandFlags("give");
	new flagszspawn = GetCommandFlags("z_spawn");
	new flagsupgradeadd = GetCommandFlags("upgrade_add");
	new flagspanic = GetCommandFlags("director_force_panic_event");
	SetCommandFlags("give", flagsgive|FCVAR_CHEAT);
	SetCommandFlags("z_spawn", flagszspawn|FCVAR_CHEAT);
	SetCommandFlags("upgrade_add", flagsupgradeadd|FCVAR_CHEAT);
	SetCommandFlags("director_force_panic_event", flagspanic|FCVAR_CHEAT);
}	